#!/bin/sh
### BEGIN INIT INFO
# Provides:          gsn
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      postgresql mysql
# Should-Stop:       postgresql mysql
# Short-Description: Start/stop gsn streaming server
### END INIT INFO

set -e

SCRIPTNAME="${0##*/}"
SCRIPTNAME="${SCRIPTNAME##[KS][0-9][0-9]}"

GSN_CONTROL_PORT=22232
GSN_FOLDER="/opt/gsn/1.1.8"
GSN_PIDFILE="/var/run/gsn_1.1.8.pid"

JAVA_HOME="/usr"

if [ ! -x "$JAVA_HOME/bin/java" ] ; then
	echo "No JRE/JDK installed"
	exit 0
fi
if [ ! -e "$GSN_FOLDER/lib/gsn-1.1.8.jar" ] ; then
    echo "GSN is not installed in the directory $GSN_FOLDER"
    exit 0
fi

. /lib/lsb/init-functions

test -f /etc/default/rcS && . /etc/default/rcS

if [ -f /etc/default/gsn ] ; then
	. /etc/default/gsn
fi

pidof_gsn() {
	# if there is actually a java process whose pid is in GSN_PIDFILE,
	# print it and return 0.
	if [ -e "$GSN_PIDFILE" ]; then
		if pidof java | tr ' ' '\n' | grep -w $(cat $GSN_PIDFILE); then
			return 0
		fi
	fi
	return 1
}

gsn_wait_stop() {
	# running ?
	PIDTMP=$(pidof_gsn) || true
	if kill -0 "${PIDTMP:-}" 2> /dev/null; then
	    PID=$PIDTMP
	fi
         
        old_PWD=$PWD
        cd $GSN_FOLDER
	$JAVA_HOME/bin/java -classpath "./conf/:./lib/*" gsn.GSNStop $GSN_CONTROL_PORT > /dev/null 2>&1
        cd $old_PWD
	# wait until really stopped
	if [ -n "${PID:-}" ]; then
		i=0
		while kill -0 "${PID:-}" 2> /dev/null;  do
        		if [ $i = '60' ]; then
					echo -n " timeout: killing!"
                    kill -9 "${PID:-}"
        			break;
        	 	else
        			if [ $i = '0' ]; then
                			echo -n " ... waiting "
        			else
                	      		echo -n "."
        		 	fi
        			i=$(($i+1))
        			sleep 1
        	      fi
		 done
	fi
    rm -f $GSN_PIDFILE
}

gsn_start(){
    old_PWD=$PWD
    cd $GSN_FOLDER
    PID=`$JAVA_HOME/bin/java -classpath "./conf/:./lib/*" gsn.Main $GSN_CONTROL_PORT > /dev/null 2>&1 & echo $!`
    cd $old_PWD
    echo $PID > $GSN_PIDFILE
}

case $1 in
	start)
        log_daemon_msg "Starting gsn streaming server" "gsn"
        PID=$(pidof_gsn) || true
		if [ -n "$PID" ]; then
			echo "GSN is already running (pid $PID)."
            log_end_msg 1
		else
			 gsn_start
             log_end_msg 0
		fi
	;;
	stop)
		log_daemon_msg "Stopping gsn streaming server" "gsn"

		if gsn_wait_stop; then
                        log_end_msg 0
                else
                        log_end_msg 1
                fi
	;;
	reload | force-reload | restart)

		log_daemon_msg "Restarting gsn streaming server" "gsn"
		
		PID=$(pidof_gsn) || true
		if ! gsn_wait_stop; then
                        log_end_msg 1 || true
                fi
		gsn_start

        log_end_msg 0
	;;
	status)
		PID=$(pidof_gsn) || true
		if [ -n "$PID" ]; then
			echo "GSN is running (pid $PID)."
			exit 0
		else
			echo "GSN is NOT running."
			if [ -e "$PIDFILE" ]; then
				exit 1
			else
				exit 3
			fi
		fi
	;;
	*)
		log_success_msg "Usage: /etc/init.d/gsn {start|stop|restart|reload|force-reload|status}"
		exit 1
	;;
esac
